package com.design.fitment.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.design.fitment.R;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

@ContentView(R.layout.activity_banner_detail)
public class BannerDetailActivity extends AppCompatActivity {
    @ViewInject(R.id.wv_banner)
    private WebView wvBanner;
    private String url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_banner_detail);
        ViewUtils.inject(this);
        Intent intent = getIntent();
        url = intent.getStringExtra("url");
        wvBanner.setWebViewClient(new WebViewClient()
        {
            //系统默认会通过手机浏览器打开网页，为了能够直接通过WebView显示网页，则必须设置
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                view.loadUrl(url);
                return true;
            }

            // 网页加载初始化
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon)
            {

            }

            // 网页加载结束
            @Override
            public void onPageFinished(WebView view, String url)
            {

            }

            // 加载网页出现错误
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
            {

            }
        });

        wvBanner.setWebChromeClient(new WebChromeClient()
        {
            //通知应用程序当前网页加载的进度。
            @Override
            // newProgress 加载进度
            public void onProgressChanged(WebView view, int newProgress)
            {
                Log.d("webview", "progress: " + newProgress);
            }

            @Override
            public void onRequestFocus(WebView view)
            {
                Log.d("webview", "onRequestFocus()" );
            }

            // 当关闭窗口时回调
            @Override
            public void onCloseWindow(WebView window)
            {
                super.onCloseWindow(window);
            }

            // webview中显示提示框回调
            @Override
            public boolean onJsAlert(
                    WebView view, String url, String message, JsResult result)
            {
                return super.onJsAlert(view, url, message, result);
            }

            // webview中显示对话框回调
            @Override
            public boolean onJsConfirm(
                    WebView view, String url, String message, JsResult result)
            {
                return super.onJsConfirm(view, url, message, result);
            }

            @Override
            public boolean onJsBeforeUnload(
                    WebView view, String url, String message, JsResult result)
            {
                return super.onJsBeforeUnload(view, url, message, result);
            }
            // 文件选择框打开选择文件后回调
            @Override
            public boolean onShowFileChooser(
                    WebView webView, ValueCallback<Uri[]> filePathCallback,
                    FileChooserParams fileChooserParams)
            {
                return super.onShowFileChooser(webView, filePathCallback, fileChooserParams);
            }
        });

        wvBanner.loadUrl(url);

        // 加载本地应用中的html网页
        // http:// https:// file:// ftp://
        // /android_asset - android应用中assets目录
        //mWebView.loadUrl("file:///android_asset/html/test.html");

        //String html_text = "<p align='center'>段落内容</p>";
        //mWebView.loadData(html_text,"text/html; charset=utf-8",null);

    }

    public void reload(View view)
    {
        wvBanner.reload();
    }

    public void doBack(View view)
    {
        if (wvBanner.canGoBack()) {
            wvBanner.goBack();
        }
    }

    public void doForward(View view)
    {
        if (wvBanner.canGoForward())
        {
            //mWebView.goBackOrForward();
            wvBanner.goForward();
        }
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        wvBanner.onResume();
    }

    @Override
    protected void onPause()
    {
        super.onPause();
        wvBanner.onPause();
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        wvBanner.clearCache(true);
        wvBanner.clearHistory();
        wvBanner.clearFormData();

        wvBanner.destroy();
    }
    @OnClick(R.id.iv_back)
    private void onClickBack(View view){

        this.finish();
        this.overridePendingTransition(R.anim.in_from_left, R.anim.out_from_right);
    }
}
